package com.yellow.userservice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yellow.userservice.entity.User;
import com.yellow.userservice.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
 * @author: hy
 * @create: 2024-02-19
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User selectByName(String name) {
        LambdaQueryWrapper<User>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, name);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User selectByPhoneNumber(String phoneNumber) {
        LambdaQueryWrapper<User>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhoneNumber,phoneNumber);

        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean registerUser(User user) {
        // 检查用户名是否已经存在
        User existingUser = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", user.getUserName()));
        if (existingUser != null) {
            // 用户已存在，无法注册
            log.warn(" 用户已存在，无法注册");
            return false;
        }

        // 向数据库添加新用户
        int result = userMapper.insert(user);
        return result > 0; // 如果添加成功，返回true
    }

    @Override
    public String selectAvatarUrl(String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>().eq(User::getUserName, name);
        return userMapper.selectOne(wrapper).getAvatarUrl();
    }


}
